﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.common.mvvm;
using System.Windows;
using personalplaner.business.stamm;
using personalplaner.servicemodel.dataaccess;
using System.Collections.ObjectModel;
using personalplaner.common.mvvm.common;
using personalplaner.common.plugins;
using personalplaner.common.mvvm.mediator;

namespace personalplaner.plugins.plananalysis.viewmodels
{
	//interface IPlanAnalysisPlugin
	//{
	//    Person SelectedPerson { get; }
	//}

	public class PlanAnalysisPluginViewModel : BaseViewModel//, IPlanAnalysisPlugin
	{
		public PlanAnalysisPluginViewModel(DependencyObject parent)
            : base(parent)
        {
        }

		/// <summary>
		/// Subscribe to the mediator to receive a message when a new plan was created
		/// </summary>
		/// <param name="selectedElement"></param>
		[MediatorMessageSink("ViewControlerSelectionChanged", ParameterType = typeof(UIElement))]
		public void ViewControlerSelectionChanged(UIElement selectedElement)
		{
			if (SelectedPerson == null || Views.Count <= 0 || !Views.ContainsKey(SelectedPerson))
				return;

			if (selectedElement != Views[SelectedPerson])
				SelectedPerson = null;
		}

		IList<Person> _personen;
		public IList<Person> Personen
		{
			get
			{
				if (Designer.IsDesignMode)
					return new ObservableCollection<Person>();

				if (_personen == null)
				{
					//get the data access via the service locator.
					var da = GetService<IPersonDataAccess>();
					var lst = da.GetPersonen(showAll: ShowAllPersonen);
					_personen = lst != null ? new ObservableCollection<Person>(lst) : new ObservableCollection<Person>();
				}
				return _personen;
			}
		}

		Person _selectedPerson;
		public Person SelectedPerson
		{
			get
			{
				return _selectedPerson;
			}
			set
			{
				_selectedPerson = value;
				RaisePropertyChanged("SelectedPerson");

				if (value == null)
					return;

				if (!Views.ContainsKey(value))
				{
					var view = new AnalysisView(value.PrsID);


					Views.Add(value, view);
				}

				var da = GetService<IViewControler>();
				da.SelectControl(Views[value]);
			}
		}

		Dictionary<Person, AnalysisView> _views;
		public Dictionary<Person, AnalysisView> Views
		{
			get
			{
				if (_views == null)
					_views = new Dictionary<Person, AnalysisView>();
				return _views;
			}
		}




		bool _showAllPersonen;
		public bool ShowAllPersonen
		{
			get
			{
				return _showAllPersonen;
			}
			set
			{
				_showAllPersonen = value;

                int prsid = 0;
                if (SelectedPerson != null)
                    prsid = SelectedPerson.PrsID;

				_personen = null;
				RaisePropertyChanged("ShowAllPersonen");
				RaisePropertyChanged("Personen");

                if (prsid <= 0)
                    return;

                //prsid = SelectedPerson.PrsID;
                SelectedPerson = (from p in Personen where p.PrsID == prsid select p).FirstOrDefault();
			}
		}
	}
}
